package com.huishi.ui.component;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class IncreaseSyncConfirmDialog extends JFrame {

    private String sourceName = "SourceDB"; // 源名
    private String sourceIP = "192.168.1.100"; // 源ip
    private String sourceDBName = "SourceDatabase"; // 源数据库
    private String targetName = "TargetDB"; // 目标名
    private String targetIP = "192.168.2.200"; // 目标ip
    private String targetDBName = "TargetDatabase"; // 目标数据库
    private ArrayList<String> tablesToSync = new ArrayList<>(); // 要同步的表
    private String syncStartTime = "2024-06-21 10:00:00"; // 开始同步时间
    private int interval; // 间隔时间
    private String intervalUnit = "秒"; // 间隔单位

    public IncreaseSyncConfirmDialog(JFrame parent) {
        setTitle("增量同步确认");
        setSize(600, 400);
        setLocationRelativeTo(parent);

        // 禁用最大化按钮
        setResizable(false);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        // Create panel to hold components
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // Components for displaying information
        JTextArea infoTextArea = new JTextArea();
        infoTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(infoTextArea);
        panel.add(scrollPane, BorderLayout.CENTER);

        // Confirmation button
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(e -> {
            // Show confirmation dialog
            int option = JOptionPane.showConfirmDialog(this, "Are you sure to proceed with incremental sync?", "Confirmation", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                // Perform incremental sync action
                performIncrementalSync();
            }
        });
        panel.add(confirmButton, BorderLayout.SOUTH);


        tablesToSync.add("table1");
        tablesToSync.add("table2");
        tablesToSync.add("table3");

        // Display information in the text area
        String infoText = String.format(
                "数据源: %s\n" +
                        "数据源IP: %s\n" +
                        "源数据库: %s\n\n" +
                        "目标数据源: %s\n" +
                        "目标数据源IP: %s\n" +
                        "目标数据库: %s\n\n" +
                        "同步的表:\n%s\n\n" +
                        "首次执行时间: %s\n" +
                        "同步间隔时间: %s",
                sourceName, sourceIP, sourceDBName,
                targetName, targetIP, targetDBName,
                String.join("\n", tablesToSync),
                syncStartTime, interval + intervalUnit);
        infoTextArea.setText(infoText);

        add(panel);
    }


    private static void performIncrementalSync() {
        // Placeholder method for performing incremental sync action
        JOptionPane.showMessageDialog(null, "Incremental sync performed successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        IncreaseSyncConfirmDialog increaseSyncConfirmDialog = new IncreaseSyncConfirmDialog(null);
        increaseSyncConfirmDialog.setVisible(true);
    }
}
